package com.googlecode.droidircclient;
/* =================================================
 * Droid IRC Client
 * Authors: Kevin Garton, Dennis McWherter
 * 29 September 2011
 * =================================================*/

import android.content.SharedPreferences;
import android.os.*;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;

public class GeneralPreferencesActivity extends PreferenceActivity {
	public static final String PREFS_FILE = ".droidIRC";
	private SharedPreferences prefs;
	private EditTextPreference nickedit;
	private EditTextPreference useredit;
	private EditTextPreference realedit;
	
	/**
	 * onCreate
	 * 
	 * Use the default onCreate() but also add our preferences
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.general_preferences);
		
		prefs = getSharedPreferences(PREFS_FILE, MODE_PRIVATE); /* MODE_PRIVATE keeps this exclusive? */
		nickedit = (EditTextPreference)findPreference("NicknamePref");
		useredit = (EditTextPreference)findPreference("UsernamePref");
		realedit = (EditTextPreference)findPreference("RealnamePref");
		
		loadPreferences();
	}
	
	/**
	 * onStop
	 * 
	 * Override onStop to save prefs
	 */
	@Override
	protected void onStop()
	{
		super.onStop();
		savePreferences();
	}
	
	/**
	 * loadPreferences
	 * 
	 * Load all of the preferences from the appropriate file
	 */
	private void loadPreferences()
	{
		String nick = prefs.getString("nick", "DroidIRC-Anon");
		String user = prefs.getString("user", "DroidIRC-User");
		String real = prefs.getString("real", "DroidIRC-User");
		
		// Update the visible fields
		nickedit.setText(nick);
		useredit.setText(user);
		realedit.setText(real);
	}
	
	/**
	 * savePreferences
	 * 
	 * Save all preferences
	 */
	private void savePreferences()
	{
		SharedPreferences.Editor editor = prefs.edit();
		// Save the value
		editor.putString("nick", nickedit.getText());
		editor.putString("user", useredit.getText());
		editor.putString("real", realedit.getText());
		editor.commit();
	}
}
